﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using EclipseLib.Shared.Scripting;
using System.CodeDom.Compiler;
using System.Text.RegularExpressions;

namespace Eclipse.NET_Scripter
{
    public partial class FrmMain : Form
    {

        public FrmMain()
        {
            InitializeComponent();
            ScriptBox.Margins[0].Width = 20;
            ScriptBox.Text = File.ReadAllText("DEFSC.SCR");
        }

        private void BtnCompile_Click(object sender, EventArgs e)
        {
            GameScript Script = new GameScript();
            CompilerResults Results = null;

            Script.AddAssemblyReference("System.dll");
            Script.AddAssemblyReference("EclipseLib.dll");
            Script.Code = ScriptBox.Text;
            Script.Function = "ScriptMain";

            GameAssembly Build = new GameAssembly(Script);

            Results = Build.CompileScript();

            LstErrors.Items.Clear();

            if (Results != null)
            {
                if (Results.Errors.Count > 0)
                {
                    for (int i = 0; i < Results.Errors.Count; ++i)
                        LstErrors.Items.Add(Results.Errors[i].Line.ToString() + ": " + Results.Errors[i].ErrorText);
                }
                else
                {
                    object Return = Build.Run();

                    if(Return != null)
                        MessageBox.Show(Return.ToString());
                }
            }

            Script = null;
            Results = null;
            Build = null;

            GC.Collect();
        }

        private void MnuCompile_Click(object sender, EventArgs e)
        {
            GameScript Script = new GameScript();
            CompilerResults Results = null;

            Script.AddAssemblyReference("System.dll");
            Script.AddAssemblyReference("EclipseLib.dll");
            Script.Code = ScriptBox.Text;
            Script.Function = "ScriptMain";

            GameAssembly Build = new GameAssembly(Script);

            Results = Build.CompileScript();

            LstErrors.Items.Clear();

            if (Results != null)
            {
                if (Results.Errors.Count > 0)
                    for (int i = 0; i < Results.Errors.Count; ++i)
                        LstErrors.Items.Add(Results.Errors[i].Line.ToString() + ": " + Results.Errors[i].ErrorText);
                else
                    LstErrors.Items.Add("No Errors Found!");
            }
            else
                LstErrors.Items.Add("Compile was not successful!");

            Script = null;
            Results = null;
            Build = null;

            GC.Collect();
        }

        // Export Methods
        private void MnuXmlOut_Click(object sender, EventArgs e)
        {
            SaveFileDialog DLG = new SaveFileDialog();
            DLG.Title = "Save Script to XML File";
            DLG.Filter = "XML Script File (*.xsc)|*.xsc";

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                GameScript Script = new GameScript();
                Script.Code = ScriptBox.Text;
                Script.ToXML(DLG.FileName);
                Script = null;
            }

            DLG.Dispose();
            GC.Collect();
        }
        private void MnuBinOut_Click(object sender, EventArgs e)
        {
            SaveFileDialog DLG = new SaveFileDialog();
            DLG.Title = "Save Script to Binary File";
            DLG.Filter = "Binary Script File (*.bsc)|*.bsc";

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                GameScript Script = new GameScript();
                Script.Code = ScriptBox.Text;
                Script.ToBinary(DLG.FileName);
                Script = null;
            }

            DLG.Dispose();
            GC.Collect();
        }

        // Import Method
        private void MnuImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog DLG = new OpenFileDialog();
            DLG.Title = "Import a Script File";
            DLG.Filter = "Binary Script File (*.bsc)|*.bsc| XML Script File (*.xsc)|*.xsc";
            DLG.FilterIndex = 1;

            if (DLG.ShowDialog() == DialogResult.OK)
            {
                GameScript FromFile = new GameScript();

                switch (DLG.FilterIndex)
                {
                    case 1: // Binary
                        FromFile.FromBinary(DLG.FileName);
                        break;

                    case 2: // XML
                        FromFile.FromXML(DLG.FileName);
                        break;
                }

                ScriptBox.Text = FromFile.Code;
            }

            DLG.Dispose();
            GC.Collect();
        }

        private void MnuClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MnuAbout_Click(object sender, EventArgs e)
        {
            AboutDialog DLG = new AboutDialog();
            DLG.ShowDialog();
            DLG.Dispose();
            GC.Collect();
        }
    }
}
